package com.sk.ccflower.lib.commonUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author eunwoogi
 * @since 2009. 11. 11.
 */
public class StringUtil {
	
	public static boolean isEmailPattern( String email ) {
          Pattern pattern = Pattern.compile( "\\w+[@]\\w+\\.\\w+" );
          Matcher match = pattern.matcher( email );
          return match.find();
    }
	
	public static int countTokens(String line, String delim) {
		int count = -1;

		if (line != null && delim.length() > 0) {
			/* set count to 1,, */
			count = 1;

			/* scan all the tokens,, */
			int cp = 0;
			while (true)
				if ((cp = line.indexOf(delim, cp)) != -1) {
					cp += delim.length();
					count += 1;
				} else
					break;
		} else
			count = -1;

		return count;
	}

	public static String[] toStringArray(String line, String delim) {
		int count = 0;
		String[] tokens = null;

		if (line == null || line.length() == 0)
			return null;

		/* how many tokens are there, */
		if ((count = countTokens(line, delim)) != -1) {
			int index;
			tokens = new String[count];

			/* set start/current position to zero,, */
			int sp = 0, cp = 0;

			/* derive all the tokens from the line */
			for (index = 0; index < count; index++) {
				if ((cp = line.indexOf(delim, cp)) != -1) {
					tokens[index] = line.substring(sp, cp);
					cp += delim.length();
					sp = cp;
				} else
					break;
			}

			/* set the last token,, */
			tokens[index] = line.substring(sp);
		} else {
			tokens = new String[1];
			tokens[0] = line;
		}

		return tokens;
	}

}
